package cl.DAOs;

import org.hibernate.*;
import cl.POJOs.*;
import cl.DAOs.HibernateUtil;
import java.util.List;
import org.hibernate.criterion.Order;

public class CursoDAO {

    private Session session;
    private Transaction trans;
    private List<Curso> lstCursos;
    
     public void Registrar(Curso curso) throws Exception {
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            trans = session.beginTransaction();
            session.save(curso);
            trans.commit();

        } catch (Exception ex) {
            trans.rollback();
            throw ex;
        } finally {
            session.close();
        }
    }

    public void Modificar(Curso curso) throws Exception {
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            trans = session.beginTransaction();
            session.update(curso);
        } catch (Exception ex) {
            trans.rollback();
            throw ex;
        } finally {
            session.close();
        }
    }

    public List<Curso> Listar() throws Exception {
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria cri = session.createCriteria(Curso.class);
            cri.addOrder(Order.asc("capacidad"));
            lstCursos= cri.list();
        } catch (Exception ex) {
            throw ex;

        }
        session.close();
        return lstCursos;
    }
    public List<Curso> Listartutorcurso(String rut) throws Exception {
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query =session.createQuery("from Curso where tutor_id='"+rut+"'");
            
            lstCursos= query.list();
        } catch (Exception ex) {
            throw ex;

        }
        session.close();
        return lstCursos;
    }
     public int Eliminar(Curso curso) throws Exception {
        int a = 1;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            trans = session.beginTransaction();
            session.delete(curso);
            trans.commit();

        } catch (Exception ex) {
            trans.rollback();
            a=0;
            
            
        } finally {
            session.close();
        }
        return a;
    }

    
}
